﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityModel;
using Common;
namespace Services
{
    public class CategoryService
    {
        #region***属性数据***

        ShopDbEntities bdContext = DbContextHelper.GetCurrentDbContext();


        #endregion

        #region 获取所有分类

        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        ///  PagedList<Category>
        ///  
        public IQueryable<Category> GetAllList()
        {
            return bdContext.Categories.OrderByDescending(c => c.Id);
        }

        #endregion

        #region 获取所有显示分类

        /// <summary>
        /// 获取所有显示分类
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Category> GetList()
        {
            return bdContext.Categories.Where(c => c.Status == (int)EnumModel.Status.Show).OrderByDescending(c => c.DisplayOrder);
        }
        #endregion

        #region 新增数据

        public void Create(Category model)
        {
            bdContext.Categories.AddObject(model);
            bdContext.SaveChanges();
        }
        #endregion

        #region   GetModel
        /// <summary>
        ///      GetModel
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Category GetModel(int id)
        {
            return bdContext.Categories.FirstOrDefault(c => c.Id == id);
        }
        #endregion

        #region 获取所有某Id父级下的分类

        /// <summary>
        /// 获取所有显示分类
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Category> GetListById(int id)
        {
            int parentId = GetModel(id).ParentCategoryId;
            int byId = 0;
            if (parentId == 1)
                byId = id;
            else
                byId = parentId;

            return bdContext.Categories.Where(c => c.ParentCategoryId == byId).OrderByDescending(c => c.DisplayOrder);
        }
        #endregion

        #region 获取父级名称

        /// <summary>
        /// 获取父级名称
        /// </summary>
        /// <returns></returns>
        public Category GetParentCategoryName(int id)
        {
            int byId = 0;
            int parentId = GetModel(id).ParentCategoryId;
            if (parentId == 1)
            {
                byId = id;
            }
            else
            {
                byId = parentId;
            }

            return bdContext.Categories.FirstOrDefault(c => c.Id == byId);
        }
        #endregion

        #region    递归获取分类的所有分类
        public IList<int> FindChiledRoles(IList<int> ids, Category c)
        {
            var list = bdContext.Categories.Where(m => m.ParentCategoryId == c.Id);
            foreach (var item in list)
            {
                ids.Add(item.Id);
                var childs = bdContext.Categories.Where(m => m.ParentCategoryId == item.Id);
                if (childs.Count() > 0)
                {
                    FindChiledRoles(ids, item);
                }
            }
            return ids;
        }
        #endregion
    }
}
